package lyj.springcloud.controller;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import lyj.springcloud.feign.OrderFeign;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 针对每一个接口，分别配置一个服务降级兜底方法
 */
@RestController
public class OrderController {

    @Resource
    private OrderFeign feign;

    @GetMapping("/order/payment/ok/{id}")
    public String paymentok(@PathVariable("id") String id) {
//        int i = 10/0;
        return feign.paymentok(id);
    }

    /**
     * 服务端服务降级
     *
     * @param id
     * @return
     */
    @GetMapping("/order/payment/timeout/{id}")
    @HystrixCommand(fallbackMethod = "paymentTimeoutH",commandProperties = {
            // 3秒钟以内就是正常的业务逻辑，超过3秒，超时会走定义的fallbackMethod方法
            @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000")
    })
    public String paymentTimeout(@PathVariable("id") String id) {
//        try {
//            TimeUnit.MILLISECONDS.sleep(7000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        int i = 10/0;
        return feign.paymentTimeout(id);
    }

    /**
     * 服务降级回调方法
     *
     * @param id
     * @return
     */
    public String paymentTimeoutH(@PathVariable("id") String id){
        return "我是消费者，支付系统异常或自己运行异常：" + id;
    }
}
